package com.winglinks.service.publish;

import com.winglinks.domain.ModelType;
import com.winglinks.message.MessageProductManager;
import com.winglinks.mqttclient.MqttClientFactory;
import com.winglinks.mqttclient.MyMqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author wangzhong
 * @description: 默认发送助手
 * @date 2024/1/28 15:23
 */
@Component
public class DefaultPublishService implements PublishService{

    private static final Logger log = LoggerFactory.getLogger(DefaultPublishService.class);
    @Autowired
    private MqttClientFactory mqttClientFactory;

    @Autowired
    private MessageProductManager messageProductManager;
    /**
     * 发送mqtt消息
     *
     * @param model
     * @param gatewayId
     * @param deviceIdentification
     * @param clientId
     * @param username
     * @param password
     */
    @Override
    public void publishMqttMsg(String model, String gatewayId, String deviceIdentification, String clientId, String username, String password) throws MqttException {
        String topic = messageProductManager.generateTopic(model, gatewayId);
        String msg  = messageProductManager.generateMessage(model,deviceIdentification);
        MyMqttClient mqttClient = mqttClientFactory.getClient(clientId,username,password);
        mqttClient.publish(topic,msg);
        String data = "发送了一条主题是‘"+topic+"’，内容是:"+msg;
    }
}
